//
//  MessageListTableViewCell.m
//  CarTown
//
//  Created by xionghao on 15/8/15.
//  Copyright (c) 2015年 熊浩. All rights reserved.
//

#import "MessageListTableViewCell.h"
#import "MessageListMode.h"
#import <UIImageView+WebCache.h>
#import "NSDate+Utils.h"
@interface MessageListTableViewCell ()
@property (weak, nonatomic) IBOutlet UIButton *counts;

@end


@implementation MessageListTableViewCell
+(instancetype)cellWithTablview:(UITableView *)tablview{
    MessageListTableViewCell *cell=[tablview dequeueReusableCellWithIdentifier:@"message"];
    if (cell==nil) {
        [tablview registerNib:[UINib nibWithNibName:NSStringFromClass([self class]) bundle:nil] forCellReuseIdentifier:@"message"];
        cell=[tablview dequeueReusableCellWithIdentifier:@"message"];
    }
    return cell;
}
- (void)awakeFromNib {
    // Initialization code
    self.icon.layer.cornerRadius=self.icon.width/2;
    self.icon.layer.borderColor=[UIColorWithRGB(48, 48, 48) CGColor];
    self.icon.layer.borderWidth=0.5;
    self.counts.contentEdgeInsets=UIEdgeInsetsMake(2, 2, 2, 2);
    [self.counts setBackgroundImage:[UIImage imageWithStretchableName:@"point"] forState:UIControlStateNormal];
    
}
-(void)setMode:(MessageListMode *)mode{
    _mode=mode;
    [self.icon sd_setImageWithURL:[NSURL URLWithString:[NSString getImageURLstr:_mode.ico]] placeholderImage:[UIImage imageNamed:@"usericon_placehoder"]];
    NSRange range = [_mode.content rangeOfString:@"<img src=" options:NSRegularExpressionSearch];
    
    if (range.location != NSNotFound) {
       self.detaillable.text=@"[图片]";
    }else{
         self.detaillable.text=_mode.content;
        
    }
    self.namelable.text=_mode.truename;
    if ([_mode.num integerValue]>99) {
        _mode.num=@"99";
        self.counts.hidden=NO;
    }else if ([_mode.num integerValue]==0){
        self.counts.hidden=YES;
    }else{
        self.counts.hidden=NO;
    }
    [self.counts setTitle:_mode.num forState:UIControlStateNormal];
    
    
//    NSDate *lastDate = [NSDate dateFromString:_mode.sendtime withFormat:@"yyyy-MM-dd HH:mm:ss"];
    
    self.timelable.text=
    
    
    [self changeTheDateString:_mode.sendtime];
    
}



//"08-10 晚上08:09:41.0" ->
//"昨天 上午10:09"或者"2012-08-10 凌晨07:09"
- (NSString *)changeTheDateString:(NSString *)Str
{
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    // 如果是真机调试，转换这种欧美时间，需要设置locale
//    fmt.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
    
    // 设置日期格式（声明字符串里面每个数字和单词的含义）
    // E:星期几
    // M:月份
    // d:几号(这个月的第几天)
    // H:24小时制的小时
    // m:分钟
    // s:秒
    // y:年
    fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    //    _created_at = @"Tue Sep 30 17:06:25 +0800 2014";
    
    // 微博的创建日期
    NSDate *createDate = [fmt dateFromString:Str];
    // 当前时间
    NSDate *now = [NSDate date];
    
    // 日历对象（方便比较两个日期之间的差距）
    NSCalendar *calendar = [NSCalendar currentCalendar];
    // NSCalendarUnit枚举代表想获得哪些差值
    NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
    // 计算两个日期之间的差值
    NSDateComponents *cmps = [calendar components:unit fromDate:createDate toDate:now options:0];
    
    if ([createDate isThisYear]) { // 今年
        if ([createDate isYesterday]) { // 昨天
            fmt.dateFormat = @"昨天 HH:mm";
            return [fmt stringFromDate:createDate];
        } else if ([createDate isToday]) { // 今天
            if (cmps.hour >= 1) {
                return [NSString stringWithFormat:@"%d小时前", cmps.hour];
            } else if (cmps.minute >= 1) {
                return [NSString stringWithFormat:@"%d分钟前", cmps.minute];
            } else {
                return @"刚刚";
            }
        } else { // 今年的其他日子
            fmt.dateFormat = @"MM-dd HH:mm";
            return [fmt stringFromDate:createDate];
        }
    } else { // 非今年
        fmt.dateFormat = @"yyyy-MM-dd HH:mm";
        return [fmt stringFromDate:createDate];
    }

}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
